<?php
namespace Common\Model;
use Think\Model;

/** 退款/退货 结算单
 * Class RefundModel
 * @package Common\Model
 */
class RefundModel extends Model {

    /**
     * 获取退单的详细信息
     * 订单详情
     * 退单详情
     * 退货商品信息
     * return json
     */
    public function getOrderDetail($id,$order_id)
    {

        $info = M('b2c_refund_money')->where(['id'=>$id])->find();
        //原订单详情：
        $field = 'a.bonus_pay,a.order_id,a.local_id,a.local_name,a.order_refer,a.ship_zip,a.ship_area,a.ship_addr,b.logi_no,b.money,b.logi_name,a.ship_name,a.member_id,a.ship_mobile,c.addr,a.memo,c.mobile,c.tel,c.area_text,c.addr';
        $order = M('b2c_orders')
            ->alias('a')
            ->join(C('DB_PREFIX').'b2c_delivery b on a.order_id=b.order_id', 'LEFT')
            ->join(C('DB_PREFIX').'webpos_local_store c on a.local_id=c.local_id', 'LEFT')
            ->field($field)
            ->where(['a.order_id'=>$info['order_id']])
            ->find();

        //发货货品明细
        $list1 = M('b2c_orders')
            ->alias('a')
            ->join(C('DB_PREFIX').'b2c_order_items b on b.order_id=a.order_id', 'LEFT')
            ->field('a.total_amount,a.cost_item,a.cost_freight,a.pmt_order,a.payed,b.bn,b.name,b.price,b.g_price,b.amount,b.nums')
            ->where(['a.order_id'=>$info['order_id']])
            ->select();
        if ($list1) {
            $order['total_xiaoji'] = 0;
            foreach ($list1 as &$item) {
                $item['xiaoji'] = $item['amount'];
                $order['total_xiaoji'] = bcadd($order['total_xiaoji'], $item['xiaoji'], 2);
            }
        }
        //退货单详情：
        $field = 'a.*,b.logi_no,b.money,b.logi_name,b.ship_name,a.member_id,b.ship_mobile,c.mobile,c.tel,c.area_text,c.addr,c.local_name';
        $refund = M('b2c_refund_money')
            ->alias('a')
            ->join(C('DB_PREFIX') . 'b2c_delivery b on a.order_id=b.order_id', 'LEFT')
            ->join(C('DB_PREFIX') . 'webpos_local_store c on b.local_id=c.local_id', 'LEFT')
            ->field($field)
            ->where(['a.id' => $id])
            ->find();
        $refund['store_work_logs'] = $refund['store_work_logs'] ? json_decode($refund['store_work_logs'], true) : '';
        $refund['images'] = $refund['images'] ? explode('#', $refund['images']) : '';
        //退货货品明细：
        $list2 = M('b2c_refund_money')
            ->alias('a')
            ->join(C('DB_PREFIX') . 'b2c_refund_money_goods b on a.id=b.refund_money_id')
            ->join(C('DB_PREFIX') . 'b2c_order_items c on c.item_id=b.item_id')
            ->field('a.price,a.status,a.images,a.wuliu_no,a.wuliu_name,a.message,a.comment,b.num,c.bn,c.name,c.price,c.g_price')
            ->where(['a.id' => $id,'b.order_id' => $order_id])
            ->select();

        if ($list2) {
            $refund['total_xiaoji'] = 0;
            foreach ($list2 as &$item) {
                $item['xiaoji'] = bcmul($item['price'], $item['num'], 2);
                $refund['total_xiaoji'] = bcadd($refund['total_xiaoji'], $item['xiaoji'], 2);
            }
            if (bccomp($refund['price'], $refund['total_xiaoji']) != 0) {
                $refund['yunfei_price'] = abs(bcsub($refund['price'], $refund['total_xiaoji'], 2));
            }
        }

        //获取原订单的微信支付单号 transaction_id
        $transaction_id = M('b2c_bills')->where(['order_id'=>$info['order_id'],'bill_type'=>'payments'])->getField('transaction_id');
        $order['transaction_id'] = $transaction_id;
        return $res = [
            'order' => $order,
            'refund' => $refund,
            'list1' => $list1,
            'list2' => $list2
        ];
    }

}